﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;



namespace Poetry
{
    public class Questions : MonoBehaviour
    {
        private RhymePoems rhyme_poems;
        private Poem poem;
        [HideInInspector]
        public Questions tmpQuestionBox;//当前的问题库
        [HideInInspector]
        public Question tmpQuestion;//当前问题
        public string questionName;//问题库的名字（Start()内确定题库类型）


        private void Awake()
        {
            tmpQuestionBox = new Questions(questionName ); //初始化“一东”的问题库
            tmpQuestion = tmpQuestionBox.GetQuestion(); //拿出来一个问题
        }

        void Start()
        {
            //foreach (string c in tmpQuestion.GetChoices()) //这是选项，一共12个
            //{
            //    Debug.Log(c);
            //}
            //Debug.Log(tmpQuestion.GetIssue()); //这是题目 string
            //Debug.Log(tmpQuestion.GetAnswer()); //这是正确回答 string
            //Debug.Log(tmpQuestion.IsCorrect(tmpQuestion.GetAnswer())); //传入一个string来判断回答对不对 bool
            //Console.ReadKey();
        }



        public struct Question
        {
            public string issue;
            public string[] choices;
            //public int[] answers;


            public int blank; //记录空白在哪部分
            public string blank_part; //空白的部分是啥样子
            public string[] replaced; //替换后的数组
            public string[] splited; //分句后的数组
            public Poem poem;
            public List<string> r_c; //随机文字数组
            public List<int> occupied_list; //未被占用的选字
            public System.Random r; //随机数发生器
            public int filled; //随机数

            public Question(Poem p)
            {
                poem = p;
                Console.WriteLine(poem.verse);
                r_c = new List<string>(Authology.random_char);
                occupied_list = new List<int> {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
            };
                splited = poem.verse.Split(new char[] { '，', '。' });
                r = new System.Random();


                blank = r.Next(splited.Length - 1);
                replaced = new string[splited.Length];
                Array.Copy(splited, replaced, splited.Length);
                blank_part = "";

                for (int i = 0; i < splited[blank].Length; i++)
                {
                    blank_part += "__";
                };

                replaced[blank] = blank_part;
                issue = string.Join("，", replaced);
                issue = issue.Substring(0, issue.Length - 1);
                issue += "。";

                choices = new string[12];
                filled = 0;

                for (int i = 0; i < 12; i++)
                {
                    filled = r.Next(r_c.Count);
                    choices[i] = r_c[filled];
                    r_c.RemoveAt(filled);
                };

                foreach (char c in splited[blank].ToCharArray())
                {
                    filled = r.Next(occupied_list.Count);
                    choices[occupied_list[filled]] = c.ToString();
                    occupied_list.RemoveAt(filled);
                };
            }

            public bool IsCorrect(string input)
            {
                if (input.Equals(splited[blank]))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public string GetAnswer()
            {
                return splited[blank];
            }

            public string GetIssue()
            {
                return issue;
            }

            public string[] GetChoices()
            {
                return choices;
            }
        }

        public struct Poem
        {
            public string title;
            public string author;
            public string dynasty;
            public string verse;

            public Poem(
                string n_title,
                string n_author,
                string n_dynasty,
                string n_verse
            )
            {
                title = n_title;
                author = n_author;
                dynasty = n_dynasty;
                verse = n_verse;
            }

            public Poem(Poem p)
            {
                title = p.title;
                author = p.author;
                dynasty = p.dynasty;
                verse = p.verse;
            }
        }

        public class Authology
        {
            public static List<string> random_char = new List<string>()
        {
            "暗", "昂", "艾", "翱", "罢", "伴", "榜",
            "薄", "悲", "奔", "崩", "碧", "变", "骠",
            "別", "滨", "冰", "波", "部",
        };
            public static List<Poem> yidong = new List<Poem>() {
            new Poem("荅友封见赠", "唐", "元稹", "扶床小女君先识，应为些些似外翁。"),
            new Poem("雪后宿同轨店上法护寺钟楼望月", "唐", "元稹", "烟火渐稀孤店静，月明深夜古楼中。"),
            new Poem("赠李十二牡丹花片因以饯行", "唐", "元稹", "可怜颜色经年别，收取朱阑一片红。"),
            new Poem("东湖送朱逸人归", "唐", "刘长卿", "莫道野人无外事，开田凿井白云中。"),
            new Poem("过楚宫", "唐", "李商隐", "微生尽恋人间乐，只有襄王忆梦中。"),
            new Poem("乐游原", "唐", "李商隐", "羲和自趁虞泉宿，不放斜阳更向东。"),
            new Poem("秋下荆门", "唐", "李白", "此行不为鲈鱼鲙，自爱名山入剡中。"),
            new Poem("荷叶杯 其三", "唐", "温庭筠", "小船摇漾入花里，波起，隔西风。"),
            new Poem("灯下读玄真子渔歌因怀山阴故隐追拟五首 其一", "宋", "陆游", "蘋叶绿，蓼花红，回首功名一梦中。"),
            new Poem("武侯庙古柏", "唐", "李商隐", "谁将出师表，一为问昭融。"),
            //new Poem("", "", "", ""),
        };
            public static List<Poem> erdong = new List<Poem>() {
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
        };
            public static List<Poem> sanjiang = new List<Poem>() {
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
            new Poem("", "", "", ""),
        };
        }

        public class RhymePoems
        {
            private List<Poem> poems_with_rhyme;
            private System.Random r;
            int rand;

            public RhymePoems(string rhyme)
            {
                switch (rhyme)
                {
                    case "yidong":
                        poems_with_rhyme = new List<Poem>(Authology.yidong);
                        break;
                    case "erdong":
                        poems_with_rhyme = new List<Poem>(Authology.erdong);
                        break;
                    case "sanjiang":
                        poems_with_rhyme = new List<Poem>(Authology.sanjiang);
                        break;
                    default:
                        poems_with_rhyme = new List<Poem>(Authology.yidong);
                        break;
                }
                r = new System.Random();
            }

            public Poem GetPoem()
            {
                rand = r.Next(poems_with_rhyme.Count);
                Poem return_poem = new Poem(poems_with_rhyme[rand]);
                poems_with_rhyme.RemoveAt(rand);
                return return_poem;
            }
        }

        public Questions(string rhyme)
        {
            rhyme_poems = new RhymePoems(rhyme);
        }

        public Question GetQuestion()
        {
            Question q = new Question(rhyme_poems.GetPoem());
            Debug.Log(q.GetAnswer());
            return q;
        }



    }
}


